package router

import (
	"9-13yk/client/client"
	"9-13yk/client/requset"
	"9-13yk/client/response"
	"9-13yk/common/pkg"
	"9-13yk/common/proto/user"
	"github.com/gin-gonic/gin"
	"path/filepath"
	"time"
)

func Login(c *gin.Context) {
	var data requset.Login
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	login, err := client.Login(c, &user.LoginReq{
		Phone:    data.Phone,
		Password: data.Password,
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, login)
}
func ProductList(c *gin.Context) {
	var data requset.ProductList
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	list, err := client.ProductList(c, &user.ProductListReq{
		MealProductsTypes: int64(data.Types),
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, list)
}

func Upload(c *gin.Context) {
	file, m, err := c.Request.FormFile("file")
	if err != nil {
		return
	}
	if m.Size >= 5*1024*1024 {
		response.ResponseError(c, "文件过大")
		return
	}
	ext := filepath.Ext(m.Filename)
	m2 := map[string]bool{
		".png": true,
		".jpg": true,
	}
	if !m2[ext] {
		response.ResponseError(c, "文件类型错误")
		return
	}
	s := time.Now().Format("20060102150405") + ext
	upload := pkg.Upload(file, m, s)
	response.ResponseSucces(c, upload)
}

func SettlementAdd(c *gin.Context) {
	ui := c.GetUint("userId")
	var data requset.SettlementAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	add, err := client.SettlementAdd(c, &user.SettlementAddReq{
		UserId:            int64(ui),
		MealProductsId:    int64(data.MealProductId),
		MealProductsNotes: data.MealProductNotes,
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, add)
}

func OrderAdd(c *gin.Context) {
	ui := c.GetUint("userId")
	var data requset.OrderAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	add, err := client.OrderAdd(c, &user.OrderAddReq{
		UserId:         int64(ui),
		MealProductsId: int64(data.MealProductsId),
		Settlement:     int64(data.SettlementId),
		CouponId:       int64(data.CouponId),
		PayType:        int64(data.PayType),
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, add)
}

func PayCall(c *gin.Context) {
	trade_status := c.Request.FormValue("trade_status")
	out_trade_no := c.Request.FormValue("out_trade_no")
	call, err := client.PayCall(c, &user.PayCallReq{
		OrderSn: out_trade_no,
		Status:  trade_status,
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, call)
}

func GetOrderList(c *gin.Context) {
	list, err := client.GetOrderList(c, &user.GetOrderListReq{})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, list)
}
func CouPonList(c *gin.Context) {
	list, err := client.CouPonList(c, &user.CouponListReq{})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, list)
}
func CouPonAdd(c *gin.Context) {
	ui := c.GetUint("userId")
	var data *requset.CouponAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	add, err := client.CouPonAdd(c, &user.CouponAddReq{
		CouPonsId: int64(data.CouPonsId),
		UserId:    int64(ui),
	})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, add)
}

func MyCouponList(c *gin.Context) {
	list, err := client.MyCouPonList(c, &user.MyCouponListReq{})
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	response.ResponseSucces(c, list)
}
git 